package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Detectfeature;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("DetectfeatureDAO")
@Transactional
public class DetectfeatureDAOImpl extends AbstractJpaDao<Detectfeature> implements DetectfeatureDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Detectfeature.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public DetectfeatureDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Detectfeature findDetectfeatureById(Integer id) throws DataAccessException {

		return findDetectfeatureById(id, -1, -1);
	}

	@Transactional
	public Detectfeature findDetectfeatureById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectfeatureById", startResult, maxRows, id);
			return (Detectfeature) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detectfeature> findDetectfeatureByName(String name) throws DataAccessException {

		return findDetectfeatureByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectfeature> findDetectfeatureByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectfeatureByName", startResult, maxRows, name);
		return new LinkedHashSet<Detectfeature>(query.getResultList());
	}

	@Transactional
	public Set<Detectfeature> findDetectfeatureByNameContaining(String name) throws DataAccessException {

		return findDetectfeatureByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectfeature> findDetectfeatureByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectfeatureByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Detectfeature>(query.getResultList());
	}

	@Transactional
	public Set<Detectfeature> findDetectfeatureByQuantity(BigDecimal quantity) throws DataAccessException {

		return findDetectfeatureByQuantity(quantity, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectfeature> findDetectfeatureByQuantity(BigDecimal quantity, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectfeatureByQuantity", startResult, maxRows, quantity);
		return new LinkedHashSet<Detectfeature>(query.getResultList());
	}

	@Transactional
	public Set<Detectfeature> findDetectfeatureByDescription(String description) throws DataAccessException {

		return findDetectfeatureByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectfeature> findDetectfeatureByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectfeatureByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Detectfeature>(query.getResultList());
	}

	@Transactional
	public Set<Detectfeature> findDetectfeatureByDescriptionContaining(String description) throws DataAccessException {

		return findDetectfeatureByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectfeature> findDetectfeatureByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectfeatureByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Detectfeature>(query.getResultList());
	}

	@Transactional
	public Detectfeature findDetectfeatureByPrimaryKey(Integer id) throws DataAccessException {
		return findDetectfeatureByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Detectfeature findDetectfeatureByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectfeatureByPrimaryKey", startResult, maxRows, id);
			return (Detectfeature) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detectfeature> findDetectfeaturesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectfeaturesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detectfeature> findAllDetectfeatures() throws DataAccessException {

		return findAllDetectfeatures(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectfeature> findAllDetectfeatures(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllDetectfeatures", startResult, maxRows);
		return new LinkedHashSet<Detectfeature>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Detectfeature> findDetectfeaturesByDetect(Integer detectId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllDetectfeaturesByDetect", start, max, detectId);
		return new LinkedHashSet<Detectfeature>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Detectfeature> findDetectfeaturesByFeature(Integer featureId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllDetectfeaturesByFeature", start, max, featureId);
		return new LinkedHashSet<Detectfeature>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Detectfeature> findDetectfeaturesByUnit(Integer unitId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllDetectfeaturesByUnit", start, max, unitId);
		return new LinkedHashSet<Detectfeature>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectfeature> findDetectfeatureByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Detectfeature>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Detectfeature> findDetectfeaturesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Detectfeature.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Detectfeature> detectfeatures = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return detectfeatures;
	}

	@Transactional
	public Detectfeature findDetectfeatureByPhysicalPrimaryKey(Integer detectId, Integer featureId)
			throws DataAccessException {
		return findDetectfeatureByPhysicalPrimaryKey(detectId, featureId, -1, -1);
	}

	@Transactional
	public Detectfeature findDetectfeatureByPhysicalPrimaryKey(Integer detectId, Integer featureId, int startResult,
			int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectfeatureByPhyscialPrimaryKey", startResult, maxRows, detectId,
					featureId);
			return (Detectfeature) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Detectfeature entity) {
		return true;
	}
}
